package com.jian.sys.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jian.sys.pojo.Permission;
import com.jian.sys.service.PermissionService;
import com.jian.sys.service.RoleService;
import com.jian.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class UserGetPermission {
    @Autowired
    @Lazy
    PermissionService permissionService;
    @Autowired
    RoleService roleService;
    @Autowired
    UserService userService;
    public  List<Permission>  getUserPermission(Integer userId){
        List<Permission> list = null;
        //获取用户的角色ID
        List<Integer>roleIds=this.userService.queryUserRoleByUid(userId);
        //通过角色ID获取权限和菜单
        Set<Integer> pIds=new HashSet<>();
        for (Integer roleId : roleIds) {
            List<Integer> permissionId=this.roleService.queryRolePermissionByRid(roleId);
            for (Integer pid : permissionId) {
                pIds.add(pid);
            }
        }
        if (pIds.size()>0){
            QueryWrapper<Permission> qw = new QueryWrapper<>();
            qw.in("id",pIds);
            qw.eq("type",Constant.TYPE_MENU);
            list = this.permissionService.list(qw);
        }else {
            list =new ArrayList<>();
        }
        return list;
    }
}
